﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Web;
using MongoDB.Bson;
using MongoDB.Driver;
using ValuationResource;
using ValuationLogic;
using System.IO;
namespace ValuationModel
{
    public class PriceReader
    {
        public DateTime getVersionDate()
        {
            try
            {
                string fileName = "c:\\versionDateTable.txt";
                StreamReader srVersion = new StreamReader(fileName, Encoding.Default);
                DateTime myDateTime = System.Convert.ToDateTime(srVersion.ReadLine());
                return myDateTime;
            }
            catch
            {
                return new DateTime(9998, 7, 28);
            }
        }

        public bool savePrice(Hashtable settlementTable, Hashtable contractTable, DateTime versionDate)
        { 
            //Note:数据结构settlementTable[year*10000 + month*100 + day]
            //Note:contractTable[today.month * 10^8 + today.day * 10^6 + today.year * 10^4 + Contract.year * 100 + Contract.month]
            DateTime startToday = Logic.getInternetTime();
            startToday = startToday.AddDays(-1);
            string sToday = startToday.ToShortDateString();
            int year = System.Convert.ToInt32(DateTime.Now.Year.ToString());
            int month = System.Convert.ToInt32(DateTime.Now.Month.ToString());
            int flag = sToday.IndexOf("/");
            string n = sToday.Substring(flag + 1, sToday.Length - flag - 1);
            flag = n.IndexOf("/");
            string sday = n.Substring(flag + 1, n.Length - flag - 1);
            int day = System.Convert.ToInt32(sday); 
            int today = year * 10000 + month * 100 + day;
            //获取今天的int型年月日“20130703”，已测试
////use this:
            int theMonth = DateTime.Now.Month;
            int theDay = DateTime.Now.Day;
///
            string weekNo = startToday.DayOfWeek.ToString();
            int conYear = year%100;
            int conMonth = month, conMonth_BK = conMonth;
            try
            {
                DateTime startDay = Logic.getInternetTime();
               //for(int now = System.Convert.ToInt32(startDay);;)//settlementTable
                while (true)
                {
                    startDay = startDay.AddDays(-1);
                    while (startDay.DayOfWeek.ToString()[0] == 'S')
                        startDay = startDay.AddDays(-1);
                    int dateValue = startDay.Year * 10000 + startDay.Month * 100 + startDay.Day;
                    if ((startDay.DayOfWeek.ToString()[0] != 'S') && (settlementTable[dateValue] == null))
                        break;
                    else
                    {
                        string myPath = @"c:\settlementTable.txt";
                        StreamWriter sw = new StreamWriter(myPath, true);
                        sw.WriteLine("{0}:{1}", dateValue, settlementTable[dateValue]);
                        sw.Close();
                        conMonth = conMonth_BK;
                        for (int nowYear = conYear; nowYear <= 99; nowYear++)
                        {
                            for (int nowMon = conMonth + 1; nowMon <= 12; nowMon++)
                            {
                                int thisKey;
                                int thisYear = year % 100;
                                thisKey = startDay.Month * 100000000 + startDay.Day * 1000000 + (startDay.Year - 2000) * 10000 + nowYear * 100 + nowMon;
                                if (contractTable[thisKey] == null)
                                    break;
                                else
                                {
                                    string myPath2 = @"c:\contractTable.txt";
                                    StreamWriter sw2 = new StreamWriter(myPath2, true);
                                    sw2.WriteLine("{0}:{1}", thisKey, contractTable[thisKey]);
                                    sw2.Close();
                                }
                            }
                            conMonth = 0;//new year round
                        }
                    }
                }

///存储版本号：
                string myPath3 = @"c:\versionDateTable.txt";
                StreamWriter sw3 = new StreamWriter(myPath3, true);
                sw3.WriteLine("{0}", versionDate);
                sw3.Close();

                return true;
            }
            catch
            {
                return false;
            }
        }
        public bool restorePrice(ref Hashtable settleTable, ref Hashtable contractTable)
        {
            try
            {
                string fileName = "c:\\settlementTable.txt";
                StreamReader sr = new StreamReader(fileName, Encoding.Default);
                string nn;
                while ((nn = sr.ReadLine()) != null)
                {
                    int flag = nn.IndexOf(":");
                    int theKey = System.Convert.ToInt32(nn.Substring(0,flag));
                    double theValue = System.Convert.ToDouble(nn.Substring(flag + 1, nn.Length - flag -1));
                    settleTable.Add(theKey, theValue);
                }
                string fileName2 = "c:\\contractTable.txt";
                StreamReader sr2 = new StreamReader(fileName2, Encoding.Default);
                string nn2;
                while ((nn2 = sr2.ReadLine()) != null)
                {
                    int flag = nn2.IndexOf(":");
                    int theKey = System.Convert.ToInt32(nn2.Substring(0, flag));
                    double theValue = System.Convert.ToDouble(nn2.Substring(flag + 1, nn2.Length - flag - 1));
                    contractTable.Add(theKey, theValue);
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
